<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2022-08-15 16:45:26
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2022-12-21 17:10:12
 * @FilePath: \grazlink20\uc_sys\src\views\Asset\Pasture\components\See.vue
 * Copyright (c) 2022 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElButton } from 'element-plus'

import { ContentWrap } from '@/components/ContentWrap'
import { Descriptions } from '@/components/Descriptions'
import { UploadMultiple } from '@/components/UploadMultiple'
import { Dialog } from '@/components/Dialog'
import { Table } from '@/components/Table'

// 获取字典

import { useCache } from '@/hooks/web/useCache'
import { zcInventoryDetailedListApi } from '@/api/cy/Pan'
import { useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useTable } from '@/hooks/web/useTableTest'
const { wsCache } = useCache()

/****************************** 变量 start ******************************/
const dict: any = wsCache.get('dict')
// const data = ref<any>({})
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const dialogVisible = ref(false)
const dialogTitle = ref('')

const dFile = ref([])

const d_columns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    width: 30
  },
  {
    field: 'enclosure_name',
    label: '栏位',
    width: 150
  },
  {
    field: 'asset_code',
    label: '注射编码',
    width: 200
  },
  {
    field: 'asset_code_2',
    label: '项圈编码',
    width: 200
  },
  {
    field: 'earcon_code',
    label: '耳标号',
    width: 200
  },
  {
    field: 'breed',
    label: '畜别',
    width: 100,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed', cellValue)
    }
  },
  {
    field: 'breed_sub',
    label: '品种',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('breed_sub', cellValue)
    }
  },
  {
    field: 'purpose',
    label: '用途',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('purpose', cellValue)
    }
  },
  {
    field: 'sex',
    label: '性别',
    width: 150,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('animal_sex', cellValue)
    }
  },
  {
    field: 'should_number',
    label: '应盘数',
    width: 100
  },
  {
    field: 'actual_number',
    label: '实盘数',
    width: 100
  },
  // {
  //   field: 'difference_number',
  //   label: '差异',
  //   width: 100
  // },
  {
    field: 'create_time',
    label: '创建日期',
    width: 180
  }
  // {
  //   field: 'state',
  //   label: '状态',
  //   width: 150
  // },
  // {
  //   field: 'note',
  //   label: '备注',
  //   width: 150
  // },
  // {
  //   field: 'medical_user_name',
  //   label: '兽医名称',
  //   width: 150
  // },
  // {
  //   field: 'creation_time',
  //   label: '创建时间',
  //   width: 180
  // },
  // {
  //   field: 'action',
  //   width: 60,
  //   fixed: 'right',
  //   label: '操作'
  // }
])
/****************************** 变量 end ******************************/
const { allSchemas } = useCrudSchemas(d_columns)

const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: zcInventoryDetailedListApi,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { getList } = methods

/****************************** 组件参数 start ******************************/
// 获取参数
const props = defineProps({
  currentRow: {
    type: Object,
    default: () => null
  },
  detailSchema: {
    type: Array,
    default: () => []
  },
  id: {
    type: String,
    default: null
  }
})
/****************************** 组件参数 end ******************************/

/****************************** 函数方法 start ******************************/

/****************************** 函数方法 end ******************************/
const newFixedSearch = ref({})
/****************************** 初始化 start ******************************/
const init = async () => {
  // 调用查看接口查询数据
  // await getMedicalOrderSee()
  newFixedSearch.value = {
    inventory_id: props.id
  }
  tableObject.fixedSearch = newFixedSearch.value
  getList()
}
init()
/****************************** 初始化 end ******************************/
</script>

<template>
  <div>
    <ContentWrap title="主数据">
      <Descriptions :schema="props.detailSchema" :data="props.currentRow">
        <template #inventory_rule_type="{ row }: { row: any }">
          <div>{{ getDictLabel('inventory_type', row.inventory_rule_type) }}</div>
        </template>
        <template #state="{ row }: { row: any }">
          <div>{{ getDictLabel('inventory_state', row.state) }}</div>
        </template>
      </Descriptions>
    </ContentWrap>

    <ContentWrap title="明细数据">
      <Table
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
        :columns="allSchemas.tableColumns"
        :data="tableObject.tableList"
        :loading="tableObject.loading"
        :border="true"
        :pagination="{
          total: tableObject.count
        }"
        @register="register"
        :selection="false"
      />
    </ContentWrap>

    <Dialog v-model="dialogVisible" :title="dialogTitle">
      <div>
        <UploadMultiple
          ref="dFileRef"
          v-model="dFile"
          :response="{
            url: 'file_url',
            name: 'file_name',
            file_type: 'file_type'
          }"
          disabled
        />
      </div>

      <template #footer>
        <ElButton @click="dialogVisible = false">关闭</ElButton>
      </template>
    </Dialog>
  </div>
</template>
